//
//  ViewHelper.m
//  ParkingManager
//
//  Created by zhangpei on 14/12/2.
//  Copyright (c) 2014年 ADTEC. All rights reserved.
//

#import "ViewHelper.h"
#import "InsetsTextField.h"
#import "NonePerformActionTextField.h"


@implementation ViewHelper
#pragma mark - UIImageView
#pragma mark -
//生成纯色图片
+ (UIImage *)createImageWithColor:(UIColor *)color withSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
    [color setFill];
    [bezierPath fill];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark - UIImageView
#pragma mark -
+ (UIImageView *)superView:(UIView *)superView withFrame:(CGRect)frame withImage:(UIImage *)image withBackgroundColor:(UIColor *)backgroundColor withUserInteractionEnabled:(BOOL)enabled
{
    //图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = image;
    imageView.backgroundColor = backgroundColor;
    imageView.userInteractionEnabled = enabled;
    [superView addSubview:imageView];
    return imageView;
}

#pragma mark - UITextField
#pragma mark -
+ (UITextField *)superView:(UIView *)supView withTextFieldType:(TextFieldType)textFieldType withFrame:(CGRect)frame withKeyboardType:(UIKeyboardType)keyboardType withPlaceholder:(NSString *)placeholder withSecureTextEntry:(BOOL)secureTextEntry withText:(NSString *)text withReturnKeyType:(UIReturnKeyType)returnKeyType withDelegate:(id)delegate withBackgroundColor:(UIColor *)backgroundColor withTextColor:(UIColor *)textColor withClearButtonMode:(UITextFieldViewMode)mode withFont:(UIFont *)font withTextAlignment:(NSTextAlignment)textAlignment
{
    
//    NSArray *textTypeStringAry = @[@"UITextField",@"InsetsTextField",@"NonePerformActionTextField",@"PassGuardTextField",@"InsetsPassGuardTextField"];
    
    UITextField *textField = nil;
    switch (textFieldType) {
        case TextFieldTypeOfDefault:
            textField = [[UITextField alloc] initWithFrame:frame];
            break;
        case TextFieldTypeOfInsets:
            textField = [[InsetsTextField alloc] initWithFrame:frame];
            break;
        case TextFieldTypeOfNonePerformAction:
            textField = [[NonePerformActionTextField alloc] initWithFrame:frame];
            break;
        default:
            
            break;
    }
    
    
    textField.keyboardType = keyboardType;
    textField.placeholder = placeholder;
    textField.secureTextEntry = secureTextEntry;
    textField.text = text;
    textField.returnKeyType = returnKeyType;
    textField.delegate = delegate;
    textField.backgroundColor = backgroundColor;
    textField.textColor = textColor;
    textField.clearButtonMode = mode;
    textField.font = font;
    textField.textAlignment = textAlignment;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [supView addSubview:textField];
    return textField;
    
    
}

#pragma mark - UILabel
#pragma mark -
+ (UILabel *)superView:(UIView *)supView withFrame:(CGRect)frame  withText:(NSString *)text withBackgroundColor:(UIColor *)backgroundColor withTextColor:(UIColor *)textColor withFont:(UIFont *)font withTextAlignment:(NSTextAlignment)textAlignment
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.backgroundColor = backgroundColor;
    label.textColor = textColor;
    label.font = font;
    label.textAlignment = textAlignment;
    [supView addSubview:label];
    return label;
}


#pragma mark - UIButton
#pragma mark -
+ (UIButton *)superView:(UIView *)superView withFrame:(CGRect)frame withNormalImage:(UIImage *)normalImage withNormalBackgroundImage:(UIImage *)normalBackgroundImage withBackgroundColor:(UIColor *)backgroundColor withTitle:(NSString *)title withFont:(UIFont *)font withTag:(NSInteger)tag withTitleColor:(UIColor *)titleColor withTitleAlignment:(NSTextAlignment)titleAlignment
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setFrame:frame];
    
    [button setImage:normalImage forState:UIControlStateNormal];
    
    [button setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
    
    //button.backgroundColor = [];
    if (backgroundColor != nil)
    {
        //生成图片
        UIGraphicsBeginImageContextWithOptions(button.bounds.size, NO, 0);
        UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect:button.bounds];
        [backgroundColor setFill];
        [bezierPath fill];
        UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
    
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = font;
    button.tag = tag;
    button.titleLabel.textAlignment = titleAlignment;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [superView addSubview:button];
    return button;
}


@end
